//
//  YLCollectionCalculatorViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/15.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLCollectionCalculatorViewModel.h"
#import "YLGlobeHeader.h"
#import "YLProductModel.h"
@implementation YLCollectionCalculatorViewModel



- (void)setMoney:(NSInteger)money
{
    _money = money;

    YLCollectionCalculatorGroup *group;
    
    // 预期利息
    CGFloat expectedReturn = money * ([_model.annualizedYield floatValue] / 100.0) * _model.productTerm / 12.0;
    NSString *expectedReturnStr = [NSString stringWithFormat:@"%.2f", expectedReturn];
    // 预期本息
    CGFloat expectedPrincipal = money + expectedReturn;
    
    
    NSMutableArray *arr = [NSMutableArray array];
    // 第一组
    {
        group = [[YLCollectionCalculatorGroup alloc] init];
        
        YLTitleContentModel *model = [YLTitleContentModel modelWithTitle:@"预期本息" content:
                                      [NSString stringWithFormat:@"%.2f", expectedPrincipal]
                                                            contentColor:YLMainColor hiddenLine:YES];
        model.titleFont = [UIFont boldSystemFontOfSize:15.0f];
        
        model.titleColor = YLColor(60, 60, 60);
        group.models = @[model, [YLTitleContentModel modelWithTitle:@"投资本金" content:
                           [NSString stringWithFormat:@"%.2f", (money * 1.0)]
                                                 contentColor:YLMainColor],
                          [YLTitleContentModel modelWithTitle:@"预期利息" content:expectedReturnStr
                                                 contentColor:YLMainColor],];
        [arr addObject:group];
    }
    
    
    // 分期
    NSInteger count = 0;
    {
        
        group = [[YLCollectionCalculatorGroup alloc] init];
        
        
        switch (_model.payCategory) {
            case 0:
                // 到期分配
                count = 1;
                break;
                
            case 1:
                // 按月分配
                count = _model.productTerm;
                break;
                
            case 6:
                // 半年分配
                count = _model.productTerm / 6;
                break;
        }
        
        group.title = [NSString stringWithFormat:@"还款共%02zd期，如下", count];
        
        
        [arr addObject:group];
    }
    
    
    {
        
        // 本期利息
        
        NSString *currentInterest = [NSString stringWithFormat:@"%.2f",expectedReturn / (count  * 1.0)];
        
        
        
        // 本期收款额
        NSString *currentMoney = currentInterest;
    
        
        
        CGFloat orginMoney = 0;
        
        for (NSInteger i = 0; i < count; i ++) {
        
          
            // 最后一期 本金归还
            if (i == count - 1) {
                currentInterest =  [NSString stringWithFormat:@"%.2f", [expectedReturnStr floatValue] - [currentInterest floatValue] * (count - 1)];
                orginMoney = money * 1.0;
                currentMoney = [NSString stringWithFormat:@"%.2f",expectedReturn / (count  * 1.0) + orginMoney];
            }
            
            NSInteger month = _model.productTerm / count * (i + 1);
            
                group = [[YLCollectionCalculatorGroup alloc] init];
                group.title = [NSString stringWithFormat:@"%02zd", i + 1];
                group.models = @[[YLTitleContentModel modelWithTitle:@"本期本金" content:[NSString stringWithFormat:@"%.2f", orginMoney]
                                                        contentColor:YLMainColor],
                                 [YLTitleContentModel modelWithTitle:@"本期利息" content:currentInterest
                                                        contentColor:YLMainColor],
                                 [YLTitleContentModel modelWithTitle:@"本期收款额" content:currentMoney
                                                        contentColor:YLMainColor],
                                 [YLTitleContentModel modelWithTitle:@"预期收款日期" content:[self getMonth:month]]];
                [arr addObject:group];
            
        }
    
    }

    _groups = arr;
}

- (NSString *)getMonth:(NSInteger)month
{
    NSCalendar *calender2 = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    [calender2 setFirstWeekday:2];// 国外是从周日 开始算的 我们是周一 所以 写了2
    
   // NSDateComponents *components2 = nil;
    
    NSDate *currentDate = [NSDate date];
    
    
//    NSTimeZone * zone = [NSTimeZone systemTimeZone];
//
//    NSInteger interval = [zone secondsFromGMTForDate:currentDate];
    
    NSDate * nowDate = currentDate;
    
 //   components2 = [calender2 components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:nowDate];
    
    NSDateComponents *adcomps = [[NSDateComponents alloc]init];
    
    [adcomps setYear:0];
    
    [adcomps setMonth:+month];
    
    [adcomps setDay:-1];
    
    NSDate *newdate = [calender2 dateByAddingComponents:adcomps toDate:nowDate options:0];
    
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    return [dateFormatter stringFromDate:newdate];
}


- (NSArray *)groups
{
    if (!_groups) {
        NSMutableArray *arr = [NSMutableArray array];
        _groups = arr;
    
    }
    return _groups;
}



@end
